package com.aaa.power.controller;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.Menu;
import com.aaa.power.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2020-11-09 20:05:04
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        if(id==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }

        return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg()
        ,this.menuService.queryById(id));
    }

    @GetMapping("selectMenuTree")
    public Result selectMenuTree() {
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.menuService.queryAllToTreeData());
    }

    /**
     * 根据token查询用户信息，再根据用户ID查询该用户的拥有的全选菜单
     * @param token
     * @return
     */
    @GetMapping("queryMenuTreeByToken")
    public Result selectMenuTreeByToken(String token){
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.menuService.selectMenuByToken(token));
    }

    @PostMapping("insertMenu")
    public Result insertMenu(@RequestBody Menu menu){
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.menuService.insert(menu));
    }

    @DeleteMapping("deleteMenu")
    public Result deleteMenu(Integer id){
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.menuService.deleteById(id));
    }

    @PutMapping("updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.menuService.update(menu));
    }

    /**
     * 查询非空可用url
     * @return
     */
    @GetMapping("queryUrlNotNull")
    public Result queryUrlNotNull(){
        return menuService.queryUrlNotNull();
    }




}